package com.herench.bs.transfer;

import com.herench.bs.utils.DataFormatUtils;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * com.herench.bs.transfer.transfer
 *
 * @author wangwenkai
 * @date 2017/10/18/14:33
 * E-mail：605064334@qq.com
 * 预约报到交易撤销
 */
public class CanceleAppointTransactionTransfer extends AbstractTransfer {

    public CanceleAppointTransactionTransfer(String method, Client client,String messageMethod)  {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idCard = (String) params.get("idCard");
        String appointId = (String) params.get("appointId");
        String patientId = (String)params.get("patientId");
        String visitNo = (String)params.get("visitNo");
        String visitDate = DataFormatUtils.getSimpleTimeString((Date) params.get("appointDate"));
        String tranNo = (String)params.get("tranNo");
        String rcptNo = (String)params.get("rcptNo");
        //03:预约报到
        String type = "03";
        //0：自费时传0
        String insurLimits = "0";
        String insurNo = "";
        String insurPwd = "";
        String insurVisitId = "";
        //自费：7
        String insuranceType = "7";
        String payType = "";
        String mtType = "";
        String insurPay = "";
        String accountPay = "";
        String ownPay = "";
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Regist").addElement("DataRow");
        dataRowElement.addAttribute("PERSON_NO", idCard).addAttribute("PATIENT_ID", patientId).addAttribute("VISIT_NO", visitNo);
        dataRowElement.addAttribute("VISIT_DATE", visitDate).addAttribute("APPOINT_ID", appointId).addAttribute("TRAN_NO", tranNo);
        Element dataRowElementBill = tplDoc.getRootElement().element("CommitData").addElement("Bill").addElement("DataRow");
        dataRowElementBill.addAttribute("RCPT_NO", rcptNo).addAttribute("TYPE", type).addAttribute("INSUR_LIMITS", insurLimits);
        Element dataRowElementInsurance = tplDoc.getRootElement().element("CommitData").addElement("Insurance").addElement("DataRow");
        dataRowElementInsurance.addAttribute("INSUR_NO", insurNo).addAttribute("INSUR_PWD", insurPwd).addAttribute("INSUR_VISIT_ID", insurVisitId);
        dataRowElementInsurance.addAttribute("INSURANCE_TYPE", insuranceType).addAttribute("PAY_TYPE", payType).addAttribute("MT_TYPE", mtType);
        dataRowElementInsurance.addAttribute("INSUR_PAY", insurPay).addAttribute("ACCOUNT_PAY", accountPay).addAttribute("OWN_PAY", ownPay);
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        resMap.put("code", executeStatus);
        resMap.put("message", executeMessage);
        return resMap;
    }
}
